# Please install OpenAI SDK first: `pip3 install openai`

from openai import OpenAI


def dpAPI(api_key, base_url, model_name, query):
    # 1.创建一个OpenAI客户端对象
    client = OpenAI(
        api_key=api_key,  # deepseek API 密钥
        base_url=base_url  # 默认为 https://api.deepseek.com/v1
    )
    # 2.创建一个聊天会话
    response = client.chat.completions.create(
        # deepseek-chat--- 聊天模型--V3.1
        # deepseek-reasoner---- 理解模型--R1,
        model=model_name,  # 模型名称
        messages=[  # 聊天内容
            {"role": "system", "content": "我是一个AI智能助手，我可以帮助你解决各种问题。"},
            {"role": "user", "content": query},
        ],
        stream=False  # 是否流式返回结果
    )

    # 3.输出结果
    # print(response)
    # print("输出结果:", response.choices[0].message.content)  # 输出结果
    # print("推理结果:", response.choices[0].message.reasoning_content)  # 输出结果
    result = {}
    if model_name == "deepseek-reasoner":
        result["content"] = response.choices[0].message.content
        result["reasoning_content"] = response.choices[0].message.reasoning_content
    else:
        result["content"] = response.choices[0].message.content

    return result


if __name__ == '__main__':
    dp_apikey = "sk-942e904b9f1c45a1976e82a534eccbf7"
    dp_base_url = "https://api.deepseek.com/v1"
    dp_model_name = "deepseek-reasoner"
    query = "你是什么大模型"

    res = dpAPI(dp_apikey, dp_base_url, dp_model_name, query)
    print(res)
